/**
 * @brief windows的完成端口实现
 * @version 1.0
 * @author 宋炜
 * @date 2023-8-25
 */

#pragma once

#include <unordered_map>
#include <atomic>
#include <thread>
#include <mutex>

#include <winsock2.h>
#include <ws2tcpip.h>

#include "mandarava/itfc.hpp"

#if defined( _MSC_VER )
#    pragma comment( lib , "ws2_32.lib" )
#endif

class miocp : public mandaravaItfc
{
public:
	std::atomic< HANDLE >             m_cmpt_port__;
	std::atomic< int >                m_max_thds__;
	std::atomic< bool >               m_is_running__;
	std::unordered_map< HANDLE , dataCh * >     m_channels__;
	std::mutex                        m_mutex__;
public:
	miocp( int maxThd );
	virtual ~miocp();
	
	virtual bool addChannel( dataCh * ch ) final;
	/**
	 * @brief 删除通道
	 */
	virtual bool eraseChannel( dataCh * ch ) final;
	/**
	 * @brief 启动运行或者停止运行
	 * @param sw[ I ], 开关
	 * @param overtime[ I ], 等候超时时间
	 */
	virtual void run( bool sw , long overtime = 200 ) final;
private:
	void start__();
	void stop__();
};
